package com.ruoyi.game.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.constant.GameConst;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.GameEnum;
import com.ruoyi.common.utils.UserContext;
import com.ruoyi.game.domain.bo.Pick3ReqBO;
import com.ruoyi.game.domain.entity.UserPick3Record;
import com.ruoyi.game.mapper.UserPick3RecordMapper;
import com.ruoyi.game.service.IRoundService;
import com.ruoyi.game.service.IUserPick3RecordService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * 用户pick3投注信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-13
 */
@Service
public class UserPick3RecordServiceImpl implements IUserPick3RecordService
{
    @Resource
    private UserPick3RecordMapper userPick3RecordMapper;

    @Resource
    private RedisCache redisCache;

    @Resource
    private IRoundService roundService;

    @Override
    public AjaxResult go(Pick3ReqBO pick3ReqBO) {
        Date now = DateUtil.date();
        // 校验场次有效性
        AjaxResult ajaxResult = roundService.checkRound(now,pick3ReqBO.getRoundNo(), pick3ReqBO.getTimeLimit(), GameEnum.GameTypeEnum.PICK3.getType());
        if(ajaxResult.isError()){
            return ajaxResult;
        }
        BigDecimal total = NumberUtil.mul(pick3ReqBO.getMoney(),pick3ReqBO.getX());
        // TODO 校验用户余额

        // 添加用户投注记录
        UserPick3Record userPick3Record = new UserPick3Record();
        userPick3Record.setUserId(UserContext.getUserId());
        userPick3Record.setRoundNo(pick3ReqBO.getRoundNo());
        userPick3Record.setaBallNo(pick3ReqBO.getaBallNo());
        userPick3Record.setbBallNo(pick3ReqBO.getbBallNo());
        userPick3Record.setcBallNo(pick3ReqBO.getcBallNo());
        userPick3Record.setAmount(pick3ReqBO.getMoney());
        userPick3Record.setTimeLimit(pick3ReqBO.getTimeLimit());
        userPick3Record.setMulX(pick3ReqBO.getX());
        BigDecimal fee = NumberUtil.mul(total,GameConst.FEE_RATE);
        userPick3Record.setTotalAmount(NumberUtil.sub(total,fee));
        userPick3RecordMapper.insertUserPick3Record(userPick3Record);
        return AjaxResult.success();
    }

    @Override
    public List<UserPick3Record> getNotEndPick3Record(String roundNo) {
        return userPick3RecordMapper.findNotEndPick3Record(roundNo);
    }

    @Override
    public boolean updateBatch(List<UserPick3Record> userPick3Records) {
        for(UserPick3Record userPick3Record : userPick3Records){
            userPick3RecordMapper.updateUserPick3Record(userPick3Record);
        }
        return true;
    }

    @Override
    public List<UserPick3Record> page(Long userId, int pageNo, int pageSize,int timeLimit) {
        PageHelper.startPage(pageNo,pageSize,false);
        return userPick3RecordMapper.page(userId,timeLimit);
    }

    @Override
    public UserPick3Record findByRoundNo(Long userId, String roundNo) {
        UserPick3Record userPick3Record = new UserPick3Record();
        userPick3Record.setUserId(userId);
        userPick3Record.setRoundNo(roundNo);

        List<UserPick3Record> list = userPick3RecordMapper.selectUserPick3RecordList(userPick3Record);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }
}
